/**获取修改的数据**/
$(function () {
    /**初始化部门树状**/
    $.post(ctx + "dept/tree", {}, function (r) {
        if (r.code === 0) {
            var data = r.msg;
            $('#deptTree').jstree({
                "core": {
                    'data': data.children,
                    'multiple': false
                },
                "state": {
                    "disabled": true
                },
                "checkbox": {
                    "three_state": false
                },
                "plugins": ["wholerow", "checkbox"]
            });
            init_dept_edit();
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
});
/**初始化数据**/
function init_dept_edit(){
    var selected = $("#deptTable").bootstrapTreeTable("getSelections");
    var selected_length = selected.length;
    if (!selected_length) {$JuXue.n_warning('请勾选需要修改的部门！');return;}
    if (selected_length > 1) {$JuXue.n_warning('一次只能修改一个部门！');return;}
    var deptId = selected[0].id;

    $.post(ctx + "dept/getDept", {"deptId": deptId}, function (r) {
        if (r.code === 0) {
            var $deptTree = $('#deptTree');
            var dept = r.msg;
            $('#deptName').val(dept.deptName);
            $('#deptId').val(dept.deptId);
            $deptTree.jstree('select_node', dept.parentId, true);
            $deptTree.jstree('disable_node', dept.deptId);
        } else {
            $JuXue.n_danger(r.msg);
        }
    });
}

/**保存数据**/
function  editDeptInfo() {
    var ref = $('#deptTree').jstree(true);
    var deptIds = ref.get_checked();
    $("[name='parentId']").val(deptIds[0]);

    var $deptAddForm =  $("#dept-edit-form");
    $.post(ctx + "dept/update", $deptAddForm.serialize(), function (r) {
        if (r.code === 0) {
            dept_search();
            $("#dept_info").modal("hide");
            $JuXue.n_success(r.msg);
        } else $JuXue.n_danger(r.msg);
    });
}